package com.example.day04.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.day04.dao.UserMapper;
import com.example.day04.pojo.User;
import com.example.day04.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Date;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;

    @Override
    public Page<User> findAll(Integer pageIndex,String name) {
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.select(User::getId,User::getName,User::getAge,User::getSex,User::getBirth);
        queryWrapper.like(name!=null && name!="",User::getName,name);
        return userMapper.selectPage(new Page<User>(pageIndex,2),queryWrapper);
    }

    @Override
    public void add(User user) {
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        user.setBirth(new Date());
        user.setDelid(0);
        user.setVersion(0);
        userMapper.insert(user);
    }

    @Override
    public User findById(Integer id) {
        User user = userMapper.selectById(id);
        return user;
    }

    @Override
    public void delete(Integer id) {
        userMapper.deleteById(id);
    }

    @Override
    public void update(User user) {
        userMapper.selectById(user.getId());
        userMapper.updateById(user);
        userMapper.selectById(user.getId());
    }
}
